Minimum moves to reach target with rotations¶
Time: O(N); Space: O(N); hard
In an NxN grid, there is a snake that spans 2 cells and starts moving from the top left corner at (0, 0) and (0, 1). The grid has empty cells represented by zeros and blocked cells represented by ones. The snake wants to reach the lower right corner at (N-1, N-2) and (N-1, N-1).
In one move the snake can: * Move one cell to the right if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is. * Move down one cell if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is. * Rotate clockwise if it’s in a horizontal position and the two cells under it are both empty. In that case the snake moves from (r, c) and (r, c+1) to (r, c) and (r+1, c).
Rotate counterclockwise if it’s in a vertical position and the two cells to its right are both empty. In that case the snake moves from (r, c) and (r+1, c) to (r, c) and (r, c+1).
Return the minimum number of moves to reach the target.
If there is no way to reach the target, return -1.
Example 1:
Input: grid =
[
[0,0,0,0,0,1],
[1,1,0,0,1,0],
[0,0,0,0,1,1],
[0,0,1,0,1,0],
[0,1,1,0,0,0],
[0,1,1,0,0,0]
]
Output: 11
Explanation:
One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down].
Example 2:
Input: grid =
[
[0,0,1,1,1,1],
[0,0,0,0,1,1],
[1,1,0,0,0,1],
[1,1,1,0,0,1],
[1,1,1,0,0,1],
[1,1,1,0,0,0]
]
Output: 9
Notes:
2 <= n <= 100
0 <= grid[i][j] <= 1
It is guaranteed that the snake starts at empty cells.
Hints:
Use BFS to find the answer.
The state of the BFS is the position (x, y) along with a binary value that specifies if the position is horizontal or vertical.
[1]:
class Solution1(object):
"""
Time: O(n)
Space: O(n)
"""
def minimumMoves(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
level, q, lookup = 0, [(0, 0, False)], set()
while q:
next_q = []
for r, c, is_vertical in q:
if (r, c, is_vertical) in lookup:
continue
if (r, c, is_vertical) == (len(grid)-1, len(grid)-2, False):
return level
lookup.add((r, c, is_vertical))
if not is_vertical:
if c+2 != len(grid[0]) and grid[r][c+2] == 0:
next_q.append((r, c+1, is_vertical))
if r+1 != len(grid) and grid[r+1][c] == 0 and grid[r+1][c+1] == 0:
next_q.append((r+1, c, is_vertical))
next_q.append((r, c, not is_vertical))
else:
if r+2 != len(grid) and grid[r+2][c] == 0:
next_q.append((r+1, c, is_vertical))
if c+1 != len(grid) and grid[r][c+1] == 0 and grid[r+1][c+1] == 0:
next_q.append((r, c+1, is_vertical))
next_q.append((r, c, not is_vertical))
q = next_q
level += 1
return -1
[3]:
s = Solution1()
grid = [
[0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1],
[0, 0, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 0]
]
assert s.minimumMoves(grid) == 11
grid = [
[0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 0, 0]
]
assert s.minimumMoves(grid) == 9